from flask import make_response, current_app
from flask_restful.utils import PY3
from json import dumps

def output_json(data, code, headers=None):
    """Makes a Flask response with a JSON encoded body"""

    # 检查并转换 set 类型
    def convert_sets(obj):
        if isinstance(obj, set):
            return list(obj)
        elif isinstance(obj, dict):
            return {k: convert_sets(v) for k, v in obj.items()}
        elif isinstance(obj, (list, tuple)):
            return [convert_sets(item) for item in obj]
        return obj

    # 转换 data 中的 set 类型
    data = convert_sets(data)

    # 此处为自己添加***************
    if 'message' not in data:
        data = {
            'message': 'OK from muxue',
            'data': data
        }
    # **************************

    settings = current_app.config.get('RESTFUL_JSON', {})
    # If we're in debug mode, and the indent is not set, we set it to a
    # reasonable value here. Note that this won't override any existing value
    # that was set. We also set the "sort_keys" value.
    if current_app.debug:
        settings.setdefault('indent', 4)
        settings.setdefault('sort_keys', not PY3)
    # always end the json dumps with a new line
    # see https://github.com/mitsuhiko/flask/pull/1262
    dumped = dumps(data, **settings) + "\n"
    resp = make_response(dumped, code)
    resp.headers.extend(headers or {})
    return resp